{% extends "zato/index.html" %}
{% load extras %}

{% block html_title %}Pub/sub topics{% endblock %}

{% block "extra_css" %}
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.alerts.css">
{% endblock %}

{% block "extra_js" %}

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.tablesorter.min.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.cookie.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.alerts.min.js"></script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/common.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/pubsub/topic.js"></script>

    <script nonce="{{ CSP_NONCE }}">
    $.fn.zato.data_table.get_columns = function() {
        return [
            '_numbering',
            '_selection',

            'name',

            '_has_gd',
            '_max_depth',
            '_current_depth',
            '_last_pub',
            '_sub',

            '_publish',
            '_clear',
            '_edit',
            '_delete',

            'id',
            'is_internal',
            'is_active',
            'has_gd',

            'is_api_sub_allowed',
            'max_depth_gd',
            'max_depth_non_gd',
            'depth_check_freq',

            'pub_buffer_size_gd',
            'hook_service_id',
            'task_sync_interval',
            'task_delivery_interval',

            'on_no_subs_pub',
            'limit_retention',
            'limit_message_expiry',

            'limit_sub_inactivity',
        ]
    }
    </script>

{% endblock %}

{% block "content" %}
<h2 class="zato">Pub/sub topics</h2>

{% if not zato_clusters %}
    {% include "zato/no-clusters.html" %}
{% else %}

    <div id="user-message-div" style='display:none'><pre id="user-message" class="user-message"></pre></div>

{% include "zato/search-form.html" with page_prompt="Show topics"%}

    {% if cluster_id %}

        <div class='page_prompt' style="clear:left">
            <a href="javascript:$.fn.zato.pubsub.topic.create()">Create a new pub/sub topic</a>
        </div>

        <div id="markup">
            <table id="data-table">
                <thead>
                    <tr class='ignore'>
                        <th style="width:5%">&nbsp;</th>
                        <th>&nbsp;</th>

                        <th><a href="#">Name</a></th>

                        <th><a href="#">GD</a></th>
                        <th><a href="#">Max depth GD / RAM</a></th>
                        <th><a href="#">Depth GD / RAM</a></th>
                        <th><a href="#">Last pub</a></th>
                        <th>&nbsp;</th>

                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>

                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        <th class='ignore'>&nbsp;</th>
                </thead>

                <tbody>
                {% if items %}
                {% for item in items %}
                    <tr class="{% cycle 'odd' 'even' %}" id='tr_{{ item.id }}'>
                        <td class='numbering'>&nbsp;</td>
                        <td class="impexp"><input type="checkbox" /></td>
                        <td>{{ item.name }}</td>
                        <td>{{ item.has_gd|yesno:"Yes,No" }}</td>
                        <td>{{ item.max_depth_gd }} / {{ item.max_depth_non_gd }}</td>
                        <td id="current_depth_{{ item.id }}">
                            {% if item.current_depth_gd %}
                                <a href="{% url "pubsub-topic-messages" item.id item.name|slugify %}?cluster={{ cluster_id }}&amp;has_gd=True">{{ item.current_depth_gd }}</a>
                            {% else %}
                                0
                            {% endif %}
                            /
                            {{ item.current_depth_non_gd|default:0 }}
                        </td>

                        <td>
                            {% if item.last_pub_time %}

                                <a
                                    id="a_last_pub_time_{{ item.id }}"
                                    href="javascript:$.fn.zato.toggle_time('last_pub_time_{{ item.id }}', '{{ item.last_pub_time }}', '{{ item.last_pub_time_utc }}')">{{ item.last_pub_time }}
                                </a>

                                <br/>
                                <a href="{% url "pubsub-endpoint" %}?cluster={{ cluster_id }}&amp;query={{ item.last_endpoint_name }}">{{ item.last_endpoint_name }}</a>
                                <br/>

                                <a href="{% url "pubsub-message" cluster_id "topic" item.id item.last_pub_msg_id %}?has_gd={{ item.last_pub_has_gd }}{% if not item.last_pub_has_gd %}&amp;server_name={{ item.last_pub_server_name }}&amp;server_pid={{ item.last_pub_server_pid }}{% endif %}">{{ item.last_pub_msg_id }}</a>

                            {% else %}
                                <span class='form_hint'>---</span>
                            {% endif %}
                        </td>

                        <td id="sub_{{ item.id }}">
                            <a href="{% url "pubsub-subscription" %}?cluster={{ cluster_id }}&amp;topic_id={{ item.id }}">Subscriptions</a> <span class='form_hint'>({{ item.sub_count }})</span>
                        </td>

                        <td><a href="{% url "pubsub-message-publish" cluster_id item.id %}">Publish</a></td>
                        <td><a href="javascript:$.fn.zato.pubsub.topic.clear('{{ item.id }}')">Clear</a></td>
                        <td><a href="javascript:$.fn.zato.pubsub.topic.edit('{{ item.id }}')">Edit</a></td>
                        <td>
                            {% if item.is_internal %}
                                <span class="form_hint">Delete</span>
                            {% else %}
                                <a href="javascript:$.fn.zato.pubsub.topic.delete_('{{ item.id }}')">Delete</a>
                            {% endif %}
                        </td>

                        <td class='ignore item_id_{{ item.id }}'>{{ item.id }}</td>
                        <td class='ignore'>{{ item.is_internal }}</td>
                        <td class='ignore'>{{ item.is_active }}</td>
                        <td class='ignore'>{{ item.has_gd }}</td>
                        <td class='ignore'>{{ item.is_api_sub_allowed }}</td>

                        <td class='ignore'>{{ item.max_depth_gd }}</td>
                        <td class='ignore'>{{ item.max_depth_non_gd }}</td>
                        <td class='ignore'>{{ item.depth_check_freq }}</td>

                        <td class='ignore'>{{ item.pub_buffer_size_gd }}</td>
                        <td class='ignore'>{{ item.hook_service_id }}</td>
                        <td class='ignore'>{{ item.task_sync_interval }}</td>
                        <td class='ignore'>{{ item.task_delivery_interval }}</td>

                        <td class='ignore'>{{ item.on_no_subs_pub }}</td>
                        <td class='ignore'>{{ item.limit_retention }}</td>
                        <td class='ignore'>{{ item.limit_message_expiry }}</td>

                        <td class='ignore'>{{ item.limit_sub_inactivity }}</td>

                    </tr>
                {% endfor %}
                {% else %}
                    <tr class='ignore'>
                        <td colspan='25'>No results</td>
                    </tr>
                {% endif %}

                </tbody>
            </table>
        </div>

{% include "zato/paginate/action-panel.html" %}

        <div id="create-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "pubsub-topic-create" %}" method="post" id="create-form">
                    <table class="form-data">
                        <tr>
                            <td style="vertical-align:middle; width:13%">Name</td>
                            <td>{{ create_form.name }}</td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">Options</td>
                            <td>

                            <label>
                            Active {{ create_form.is_active }}
                            </label>

                            |

                            <label>
                            GD enabled {{ create_form.has_gd }}
                            </label>

                            |

                            <label>
                            API sub. allowed {{ create_form.is_api_sub_allowed }}
                            </label>

                            |

                            <label>
                            On no sub. {{ create_form.on_no_subs_pub }}
                            </label>

                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle">Max depth</td>
                            <td>

                            <label>
                            GD {{ create_form.max_depth_gd }}
                            </label>

                            |

                            <label>
                            RAM {{ create_form.max_depth_non_gd }}
                            </label>

                            |

                            <label>
                            Check freq. {{ create_form.depth_check_freq }}
                            </label>

                            </td>

                        </tr>
                        <tr>
                            <td style="vertical-align:middle">
                                Intervals
                                <br/>
                                <span class="form_hint">(in ms)</span>
                            </td>
                            <td>
                                <label>
                                    Sync
                                    {{ create_form.task_sync_interval }}
                                </label>
                                |
                                <label>
                                    Delivery {{ create_form.task_delivery_interval }}
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">
                                Limits
                                <br/>
                                <span class="form_hint">(in sec.)</span>
                            </td>
                            <td style="vertical-align:middle">
                                <label>
                                    Retention
                                    {{ create_form.limit_retention }}
                                </label>
                                |
                                <label>
                                    Expiry
                                    {{ create_form.limit_message_expiry }}
                                </label>
                                |
                                <label>
                                    Sub. inactivity
                                    {{ create_form.limit_sub_inactivity }}
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">Hook service</td>
                            <td>{{ create_form.hook_service_id }} </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:right">
                                {{ create_form.pub_buffer_size_non_gd }}
                                {{ create_form.pub_buffer_size_gd }}
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                </form>
            </div>
        </div>

        <div id="edit-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "pubsub-topic-edit" %}" method="post" id="edit-form">
                    <table class="form-data">
                        <tr>
                            <td style="vertical-align:middle; width:13%">Name</td>
                            <td>{{ edit_form.name }}</td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">Options</td>
                            <td>

                            <label>
                            Active {{ edit_form.is_active }}
                            </label>

                            |

                            <label>
                            GD enabled {{ edit_form.has_gd }}
                            </label>

                            |

                            <label>
                            API sub. allowed {{ edit_form.is_api_sub_allowed }}
                            </label>

                            |

                            <label>
                            On no sub. {{ edit_form.on_no_subs_pub }}
                            </label>

                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle">Max depth</td>
                            <td>

                            <label>
                            GD {{ edit_form.max_depth_gd }}
                            </label>

                            |

                            <label>
                            RAM {{ edit_form.max_depth_non_gd }}
                            </label>

                            |

                            <label>
                            Check freq. {{ edit_form.depth_check_freq }}
                            </label>

                            </td>

                        </tr>
                        <tr>
                            <td style="vertical-align:middle">
                            Intervals
                            <br/>
                            <span class="form_hint">(in ms)</span>
                            </td>

                            <td>

                            <label>
                            Sync
                            {{ edit_form.task_sync_interval }}
                            </label>

                            |

                            <label>
                            Delivery {{ edit_form.task_delivery_interval }}
                            </label>

                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">
                                Limits
                                <br/>
                                <span class="form_hint">(in sec.)</span>
                            </td>
                            <td>
                                <label>
                                    Retention
                                    {{ edit_form.limit_retention }}
                                </label>
                                |
                                <label>
                                    Expiry
                                    {{ edit_form.limit_message_expiry }}
                                </label>
                                |
                                <label>
                                    Sub. inactivity
                                    {{ edit_form.limit_sub_inactivity }}
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">Service</td>
                            <td>{{ edit_form.hook_service_id }} </td>
                        </tr>

                        <tr>
                            <td colspan="2" style="text-align:right">
                                {{ edit_form.pub_buffer_size_non_gd }}
                                {{ edit_form.pub_buffer_size_gd }}
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="id_edit-cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                    <input type="hidden" id="id_edit-id" name="id" />
                </form>
            </div>
        </div>

    {% endif %} {% comment %}cluster_id{% endcomment %}

{% endif %}{% comment %}not zato_clusters{% endcomment %}

{% endblock %}
